#include <fstream>

#include "Game.h"

void Game::Start() {
	//Ładujemy itemy
	if (!LoadItems()) {
		DebugMessage("Game", "Game", "Can't load items file! Quitting!", critical);
	}

	//przygotowujemy interfejs
	interface = new Interface(1024, 768);

	//próbujemy go zainicjować
	if (!interface->Init()) {
		DebugMessage("Game", "Game", "Can't init interface! Quitting!", critical);
	}

	//początkowy ekwipunek (TODO: do zmiany, może ładowany z pliku)
	Character* player = interface->getWorld()->player;
	player->armors.push_back(armors["clothes"]);
	player->armors.push_back(armors["leather-armor"]);
	player->activeArmor = player->armors.begin();
	player->weapons.push_back(weapons["dagger"]);
	player->weapons.push_back(weapons["shortsword"]);
	player->weapons.push_back(weapons["longsword"]);
	player->activeWeapon = player->weapons.begin();
	player->smallPotionsNumber = 1;
	player->bigPotionsNumber = 2;

	if (!interface->Draw(true)) {
		DebugMessage("Game", "Game", "Can't draw interface! Quitting!", critical);
	}
}

Game::~Game() {
	interface->DeInit();
	delete interface;
}

void Game::Pause() {
	isPaused = true;
}

void Game::Unpause() {
	isPaused = false;
}

bool Game::TogglePause() {
	if (isPaused) {
		Unpause();
	} else {
		Pause();
	}
	return !isPaused;
}

void Game::MainLoop() {
	while (true) {
		unsigned start = SDL_GetTicks();
		SDL_Event event;

		//zczytujemy eventy
		while(SDL_PollEvent(&event)) {
			switch(event.type) {
				case SDL_USEREVENT: { //programowe eventy (komunikacja Interface->Game)
					switch (event.user.code) {
						case e_pause: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: Pause");
							Pause();
							break;
						} case e_unpause: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: Unpause");
							Unpause();
							break;
						} case e_togglePause: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: TogglePause");
							TogglePause();
							break;
						} case e_weaponChanged: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: weaponChanged");
							interface->Draw();
							break;
						} case e_armorChanged: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: armorChanged");
							interface->Draw();
							break;
						} case e_potionUsed: {
							DebugMessage("Main", "Event", "SDL_USEREVENT: potionUsed");
						}
					}
					break;
				} case SDL_KEYDOWN: { //klawiatura
					DebugMessage("Main", "Event", "SDL_KEYDOWN");
					break;
				} case SDL_MOUSEBUTTONDOWN: { //kliknięcie
					interface->OnMouseButtonDown(event.button.x, event.button.y, event.button.button);
					break;
				} case SDL_MOUSEBUTTONUP: { //puszczenie przycisku myszy
					interface->OnMouseButtonUp(event.button.x, event.button.y, event.button.button);
					break;
				} case SDL_QUIT: { //zamknięcie okienka
					DebugMessage("Main", "Event", "SDL_QUIT");
					return;
				}
			}
		}

		if(!isPaused) interface->getWorld()->run();

		interface->Draw(true);

		unsigned end = SDL_GetTicks();
		if (end - start > loopTime) {
			DebugMessage("Game", "MainLoop", "Loop is taking to much time!", warning);
		} else {
			SDL_Delay(loopTime - end + start);
		}
	}
}

bool Game::LoadItems() {
	fstream file("data/items", ios_base::in);
	if (!file.is_open()) {
		return false;
	}

	int n;

	file >> n;
	for (int i = 0; i < n; ++i) {
		char type;
		string id;
		char name[30];
		unsigned short stat;

		file >> type >> id >> stat;
		file.getline(name, 29);

		switch (type) {
			case 'a': {
				armors[id] = Armor(name, id, stat);
				break;
			} case 'w': {
				weapons[id] = Weapon(name, id, stat);
				break;
			}
		}
	}
	return true;
}
